#include "util.h"
#include "buffer_trans.h"
#include "data.h"
#include "cache.h"
#include "bdb.h"
#define DB_FILE_SUFFIX ".dat"
struct db{
   char *dname;
   uint32 flag;
#define DB_FLAG_HASH 0
#define DB_FLAG_SEQ  1
	void *ddb;
	/*void *iter;
	int (*ddput)(void*,char*,int,char*,int);
	int(*ddget)(void*,char*,int*,struct bstring**);
	int (*dderase)(void*,char*,int);
	//void *(*ddopen)(char*,int);
	void (*ddclose)(void*);
	void* (*dditer_init)(void*,struct bstring*,int);
	int (*dditer_next)(void*,struct bstring**);
	int (*dditer_pre)(void*,struct bstring**);
	int (*dditer_close)(void*);*/
   struct harray *cdbs;//column db
   int ecode;
   int seq;
   pthread_rwlock_t rwlock;
};
struct condition{
    char *key;
	int ksiz;
	uint32 flag;
#define C_GR      0x001//>
#define C_LS      0x002//<
#define C_EQ      0x004//=
#define C_GE      0x008//>=
#define C_LE      0x010//<=
#define C_NE      0x020//!=
#define C_AND     0x040//&&
#define C_OR      0x080//||
#define C_NG      0x100//!
#define C_DEAD    0x400
	char *ref;
	int rsiz;
};
struct order{
    char *key;
	int ksiz;
#define ASC 0
#define DSC 1
	char ord;
};
struct db* dbopen(char *name,int flag);
void dbclose(struct db *gdb);
int dbdelete(struct db *gdb);
int dbputstr(struct db *gdb,char *str,int siz);
int dbput(struct db *gdb,struct harray *ha);
int dbputrec(struct db *gdb,char *arg[]);
int simpleput(struct db *gdb,char *rec[]);
struct harray* query(struct db *gdb,struct condition *c,int max,int skip,struct order *ord);
int indexadd(struct db *gdb,char *col,int csiz);
//int dberasebyid(struct db* gdb,char *id,int idsiz);
int dberase(struct db* gdb,struct condition* c);
struct harray* dbget(struct db *gdb,struct condition *conds,int nc,int max,int skip,struct order *ord);
#define addindex(gdb,col) indexadd(gdb,col,strlen(col))
#define DBNREC(db) (((struct datafile*)(db)->ddb)->nrec)
//#define dbiter_create(gdb) iter_create((gdb)->hd)
//struct harray *dbinter_next(struct iterator *iter);
//#define dbiter_del(it) iterdel(it)

struct harray* condfind(struct db *gdb,struct bdb *idb,struct condition *cond);
int checkcond(struct harray *ha,struct condition *c);
int checkconds(struct db *gdb,struct harray *ha,struct condition *conds,int nc);
void hashsort(struct harray *ha,struct order *ord);
void heapsort(struct sortitem *s,int n,int ord);
struct condition *condcreate(char *key,int ksiz,uint32 flag,char *ref,int rsiz);
